//
//  OYEmotionViewCell.swift
//  weiboCopycat
//
//  Created by Orange Yu on 2016/12/10.
//  Copyright © 2016年 Orange Yu. All rights reserved.
//

import UIKit

class OYEmotionViewCell: UICollectionViewCell {
   
    override init(frame: CGRect) {
        super.init(frame: frame)
    
        setupUI()
    }
    
    var emotionModelList:[OYEmotionModel]?{
        didSet {
            
            guard let emotionList = emotionModelList else { return  }
            
            for button in buttonList {
                // 默认情况下隐藏button
                button.isHidden = true
            }
            
            for (i,emotionModel) in emotionList.enumerated() {
                //  表情类型 0: 图片表情, 1: emoji表情
                let button = buttonList[i]
                 // MARK: -给button添加emotionModel属性
                button.emotionModel = emotionModel
                
                 // MARK: -解决复用，在button有图片的时候再将button显示
                button.isHidden = false
                if emotionModel.type == "0" {
                    // 图片表情
                    button.setTitle("", for: .normal)
                    button.setImage(UIImage(named:emotionModel.path!), for: .normal)
                } else {
                    // emoji表情
                    button.setImage(UIImage(named:""), for: .normal)
                    button.setTitle((emotionModel.code! as NSString).emoji(), for: .normal)
                }
            }
            
            
        }
    }
    var buttonList:[OYEmotionButton] = [OYEmotionButton]()
    
    /// 当前cell的IndexPath
    var indexPath:IndexPath? {
        didSet {
//            titleLabel.text = "第\(self.indexPath!.section)组，第\(self.indexPath!.item)页"
        }
    }
    
    
     // MARK: -控件的懒加载
    lazy var titleLabel:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14)
        return label
    }()
    
    lazy var deleteButton:UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named:"compose_emotion_delete"), for: .normal)
        button.setImage(UIImage(named:"compose_emotion_delete_highlighted"), for: .highlighted)
        button.addTarget(self, action: #selector(deleteAction(btn:)), for: .touchUpInside)
        button.sizeToFit()
        return button
    }()
    func setupUI() {
    
        addSubview(titleLabel)
        addSubview(deleteButton)
        
        titleLabel.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        // 为cell中添加20个button
        for _ in 0..<20 {
            let button = OYEmotionButton()
            
            button.addTarget(self, action: #selector(selectEmotion(btn:)), for: .touchUpInside)
            contentView.addSubview(button)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 34)
            buttonList.append(button)
        }
        
        
        
    }
    
     // MARK: -选中表情的点击事件
    func selectEmotion(btn:OYEmotionButton) {
        // 发送通知，将button点击事件传递给composeController处理，并将button的model传递
        NotificationCenter.default.post(name: NSNotification.Name(EmotionButtonActionNotification), object: btn.emotionModel)
    }
    
     // MARK: -删除表情的点击事件
    func deleteAction(btn:UIButton) {
        NotificationCenter.default.post(name: NSNotification.Name(DeleteButtonActionNotification), object: nil)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let buttonW:CGFloat = ScreenWidth / 7
        let buttonH:CGFloat = self.height / 3
        
        for (i,button) in buttonList.enumerated() {
            let col = i % 7
            let row = i / 7
            
            button.frame = CGRect(x: CGFloat(col) * buttonW, y: CGFloat(row) * buttonH, width: buttonW, height: buttonH)

        }
        
        deleteButton.frame = CGRect(x: self.width - buttonW, y: self.height - buttonH, width: buttonW, height: buttonH)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
}
